/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c) 2002, GeoVISTA Center
 * Licensed under Simplified BSD License
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 * @original author: jin Chen 
 * @author: jin Chen 
 * @date: Nov 10, 2008$
 * @version: 1.0
 */
package edu.psu.geovista.app.vit.pub;


import edu.psu.geovista.app.coordinator.CoordinationManager;
import edu.psu.geovista.app.dataloader.v1.DataLoaders;
import edu.psu.geovista.app.vit.pub.action.VitPubActionManager;
import edu.psu.geovista.app.vit.pub.components.ptnbskt.MyBasket;
import edu.psu.geovista.app.vit.pub.data.DefaultDataRepository;
import edu.psu.geovista.app.vit.pub.views.View;
import edu.psu.geovista.app.vit.pub.views.VitPubViewMgr;
import edu.psu.geovista.common.awt.event.dataloading.AfterLoadingEvent;
import edu.psu.geovista.common.awt.event.dataloading.AfterLoadingListener;
import edu.psu.geovista.data.model.AppDataModel;

import javax.swing.*;
import java.awt.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author jinchen
 */
public class VitPub extends javax.swing.JFrame implements AfterLoadingListener {
   public static final String ProjectConfigureRoot=".vit";

    public static final String TITLE="Visual Inquiry Toolkit";
    //components
    protected DataLoaders dataloader;

    protected JDesktopPane desktop=new JDesktopPane();
    protected CoordinationManager coordMgr;
    protected VitPubActionManager actionMgr;
    protected VitPubViewMgr viewMgr;
    DefaultDataRepository dataRepository;

    String  caseClassName;// 
    Map<Object, Integer> Id2Index;//
    /** Creates new form VitPub */
    public VitPub() {
        initComponents();
        customize();
         setUp();
    }

    private void customize() {
        getCoordMgr();
       dataRepository=new DefaultDataRepository();
       DataLoaders dataloader =getDataloader();
        dataloader.addDataSetListener(dataRepository);
        dataloader.addTsDataListener(dataRepository);
        dataloader.addAfterLoadingListener(dataRepository);
        dataloader.addAfterLoadingListener(this);
        viewMgr=new VitPubViewMgr(this);
        actionMgr=new VitPubActionManager(this);
    }

    protected void setUp(){
           Container container = this.getContentPane();
            container.setLayout(new BorderLayout());

            JDesktopPane desktop =this.getDesktop();
            container.add(desktop);

            setDesktop(desktop);

    }    
     public VitPubActionManager getActionMgr(){
           if(this.actionMgr==null){
              actionMgr=new VitPubActionManager(this);
          }
          return actionMgr;
       }

    public DataLoaders getDataloader() {
        if(dataloader==null)dataloader=new DataLoaders();
        return dataloader;
    }

    public JDesktopPane getDesktop() {
           if(desktop==null){
               desktop=new JDesktopPane();
           }
           return desktop;
       }
        public void setDesktop(JDesktopPane desktop) {
           this.desktop = desktop;
    }

    public AppDataModel getDataModel(){
       return dataRepository.getDataModel();
       
    }
     public void processAfterDataLoaded(AfterLoadingEvent e) {
         try {
             this.viewMgr.load();
             String root = viewMgr.getRoot();

            MyBasket basket = actionMgr.getBasket();
            basket.setRoot(root);
            basket.load();
         } catch (FileNotFoundException e1) {
             e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
         }
     }
     public Map<Object, Integer> getId2Index() {
        if(Id2Index==null){
            Id2Index=new HashMap<Object, Integer>();

            if (generateId2Index()) return Id2Index;

            if(Id2Index==null||Id2Index.size() ==0){
                String msg="Error: Id2Index has no information. \n SaTScan function won't work.\n"+"Please check if you have set data ID column with ObservationName.";
                JOptionPane.showMessageDialog(null,msg);
            }
        }
        return Id2Index;
    }
    private boolean generateId2Index() {

        AppDataModel dataModel = this.getDataModel();
        Object[] columnData = dataModel.getNonNumericColumnData("FIPS");
          int dataIndex=0;
        for (int i=0;i<columnData.length ;i++){
            Id2Index.put(columnData[i],dataIndex++);
        }


            //verify id Id2Index is valid
            if(Id2Index.size() ==dataModel.getRowCount()) {

                return true;
            }else{

               Id2Index.clear() ;
            }

        return false;
    }

    public CoordinationManager getCoordMgr() {
        if(coordMgr==null)coordMgr=new CoordinationManager();
        return coordMgr;
    }

    public VitPubViewMgr getViewMgr() {
        return viewMgr;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
     /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
     @SuppressWarnings("unchecked")
     // <editor-fold defaultstate="collapsed" desc="Generated Code">
     private void initComponents() {

         jSeparator3 = new javax.swing.JSeparator();
         jMenuBar1 = new javax.swing.JMenuBar();
         FMu = new javax.swing.JMenu();
         jSeparator2 = new javax.swing.JSeparator();
         Npj = new javax.swing.JMenuItem();
         jSeparator4 = new javax.swing.JSeparator();
         Spj = new javax.swing.JMenuItem();
         jSeparator1 = new javax.swing.JSeparator();
         STSMu = new javax.swing.JMenu();
         ImpSR = new javax.swing.JMenuItem();
         AddRM = new javax.swing.JMenuItem();
         showPtnBskt = new javax.swing.JMenuItem();
         CompMu = new javax.swing.JMenu();
         AddGMap = new javax.swing.JMenuItem();
         AddPcp = new javax.swing.JMenuItem();
         AddSctPlot = new javax.swing.JMenuItem();
         AddTb = new javax.swing.JMenuItem();
         AddDc = new javax.swing.JMenuItem();
         AddDMatrix = new javax.swing.JMenuItem();
         jSeparator5 = new javax.swing.JSeparator();
         rmvComp = new javax.swing.JMenuItem();
         HelpMu = new javax.swing.JMenu();
         abt = new javax.swing.JMenuItem();

         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

         FMu.setText("File");
         FMu.addActionListener(new java.awt.event.ActionListener() {
             public void actionPerformed(java.awt.event.ActionEvent evt) {
                 FMuActionPerformed(evt);
             }
         });
         FMu.add(jSeparator2);

         Npj.setText("New/Open  Project");
         Npj.addActionListener(new java.awt.event.ActionListener() {
             public void actionPerformed(java.awt.event.ActionEvent evt) {
                 NpjActionPerformed(evt);
             }
         });
         FMu.add(Npj);
         FMu.add(jSeparator4);

         Spj.setText("Save Project");
         Spj.addActionListener(new java.awt.event.ActionListener() {
             public void actionPerformed(java.awt.event.ActionEvent evt) {
                 SpjActionPerformed(evt);
             }
         });
         FMu.add(Spj);
         FMu.add(jSeparator1);

         jMenuBar1.add(FMu);

         STSMu.setText("Tools");

         ImpSR.setText("Import SaTScan Result");
         ImpSR.addActionListener(new java.awt.event.ActionListener() {
             public void actionPerformed(java.awt.event.ActionEvent evt) {
                 ImpSRActionPerformed(evt);
             }
         });
         STSMu.add(ImpSR);

         AddRM.setText("Add Reliability Map");
         AddRM.addActionListener(new java.awt.event.ActionListener() {
             public void actionPerformed(java.awt.event.ActionEvent evt) {
                 AddRMActionPerformed(evt);
             }
         });
         STSMu.add(AddRM);

         showPtnBskt.setText("Show Pattern Basket");
         showPtnBskt.addActionListener(new java.awt.event.ActionListener() {
             public void actionPerformed(java.awt.event.ActionEvent evt) {
                 showPtnBsktActionPerformed(evt);
             }
         });
         STSMu.add(showPtnBskt);

         jMenuBar1.add(STSMu);

         CompMu.setText("Components");

         AddGMap.setText("Add GeoMap");
         AddGMap.addActionListener(new java.awt.event.ActionListener() {
             public void actionPerformed(java.awt.event.ActionEvent evt) {
                 AddGMapActionPerformed(evt);
             }
         });
         CompMu.add(AddGMap);

         AddPcp.setText("Add PCP");
         AddPcp.addActionListener(new java.awt.event.ActionListener() {
             public void actionPerformed(java.awt.event.ActionEvent evt) {
                 AddPcpActionPerformed(evt);
             }
         });
         CompMu.add(AddPcp);

         AddSctPlot.setText("Add Scatterplot");
         AddSctPlot.addActionListener(new java.awt.event.ActionListener() {
             public void actionPerformed(java.awt.event.ActionEvent evt) {
                 AddSctPlotActionPerformed(evt);
             }
         });
         CompMu.add(AddSctPlot);

         AddTb.setText("Add Table");
         AddTb.addActionListener(new java.awt.event.ActionListener() {
             public void actionPerformed(java.awt.event.ActionEvent evt) {
                 AddTbActionPerformed(evt);
             }
         });
         CompMu.add(AddTb);

         AddDc.setText("Add Data Classifier");
         AddDc.addActionListener(new java.awt.event.ActionListener() {
             public void actionPerformed(java.awt.event.ActionEvent evt) {
                 AddDcActionPerformed(evt);
             }
         });
         CompMu.add(AddDc);

         CompMu.add(jSeparator5);

         rmvComp.setText("Remove Component");
         rmvComp.addActionListener(new java.awt.event.ActionListener() {
             public void actionPerformed(java.awt.event.ActionEvent evt) {
                 rmvCompActionPerformed(evt);
             }
         });
         CompMu.add(rmvComp);

         jMenuBar1.add(CompMu);

         HelpMu.setText("Help");

         abt.setText("About");
         abt.addActionListener(new java.awt.event.ActionListener() {
             public void actionPerformed(java.awt.event.ActionEvent evt) {
                 abtActionPerformed(evt);
             }
         });
         HelpMu.add(abt);

         jMenuBar1.add(HelpMu);

         setJMenuBar(jMenuBar1);

         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
         getContentPane().setLayout(layout);
         layout.setHorizontalGroup(
             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
             .addGap(0, 400, Short.MAX_VALUE)
         );
         layout.setVerticalGroup(
             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
             .addGap(0, 279, Short.MAX_VALUE)
         );

         pack();
     }// </editor-fold>
    

    private void FMuActionPerformed(java.awt.event.ActionEvent evt) {

    }

    private void NpjActionPerformed(java.awt.event.ActionEvent evt) {
        this.getDataloader().startWizard();
    }

    private void SpjActionPerformed(java.awt.event.ActionEvent evt) {
        try {
            this.viewMgr.save();
            String root = viewMgr.getRoot();

            MyBasket basket = actionMgr.getBasket();
            basket.setRoot(root);
            basket.save();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    private void ImpSRActionPerformed(java.awt.event.ActionEvent evt) {
           //Map <Object, Integer>  id2index = getId2Index();
         this.actionMgr.importSts();
    }

    private void AddRMActionPerformed(java.awt.event.ActionEvent evt) {
        actionMgr.addRMap();
    }

    private void AddGMapActionPerformed(java.awt.event.ActionEvent evt) {
        String vn = JOptionPane.showInputDialog("Please specify view name", "Map");
        this.viewMgr.createView(vn, View.TYPE_GEOMAP);
    }

    private void AddPcpActionPerformed(java.awt.event.ActionEvent evt) {
        String vn = JOptionPane.showInputDialog("Please specify view name", "PCP");
        this.viewMgr.createView(vn, View.TYPE_PCP);
    }

    private void AddSctPlotActionPerformed(java.awt.event.ActionEvent evt) {
         String vn = JOptionPane.showInputDialog("Please specify view name", "Scatter plot");
        this.viewMgr.createView(vn, View.TYPE_SCATTERPLOT);
    }

    private void AddTbActionPerformed(java.awt.event.ActionEvent evt) {
         String vn = JOptionPane.showInputDialog("Please specify view name", "Table");
        this.viewMgr.createView(vn, View.TYPE_TABLE);
    }

    private void AddDcActionPerformed(java.awt.event.ActionEvent evt) {
        String vn = JOptionPane.showInputDialog("Please specify view name","Data Classifier");
       this.viewMgr.createView(vn, View.TYPE_Classifier);
    }

    private void abtActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }
     private void showPtnBsktActionPerformed(java.awt.event.ActionEvent evt) {
        this.actionMgr.showPatternBasket() ;
    }
    private void rmvCompActionPerformed(java.awt.event.ActionEvent evt) {
        String[] vnames = viewMgr.getViewNames();
        JList viewlist=new JList();
       // ListModel listModel1 = viewlist.getModel();
       
        DefaultListModel listModel =  new DefaultListModel();
        for (int i=0;i<vnames.length ;i++){
            listModel.addElement(vnames[i]);
        }
        viewlist.setModel(listModel);
        viewlist.setSelectedIndex(0);
        JScrollPane sp=new JScrollPane(viewlist);
         Object[]  buttons=new Object[]{"OK","Cancel"};
        int value=JOptionPane.showOptionDialog(this, sp, "Remove"+" a view",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.PLAIN_MESSAGE ,
                null,
                buttons,
                buttons[0]);
       if(value==JOptionPane.OK_OPTION){
           String  vn = (String) viewlist.getSelectedValue();
           viewMgr.removeView(vn);
       }
        
    }
    private void AddDMatrixActionPerformed(java.awt.event.ActionEvent evt) {
         String vn = JOptionPane.showInputDialog("Please specify view name","Dendro-Matrix");
       this.viewMgr.createView(vn, View.TYPE_DendroMatrix);
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VitPub().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JMenuItem AddDMatrix;
    private javax.swing.JMenuItem AddDc;
    private javax.swing.JMenuItem AddGMap;
    private javax.swing.JMenuItem AddPcp;
    private javax.swing.JMenuItem AddRM;
    private javax.swing.JMenuItem AddSctPlot;
    private javax.swing.JMenuItem AddTb;
    private javax.swing.JMenu CompMu;
    private javax.swing.JMenu FMu;
    private javax.swing.JMenu HelpMu;
    private javax.swing.JMenuItem ImpSR;
    private javax.swing.JMenuItem Npj;
    private javax.swing.JMenu STSMu;
    private javax.swing.JMenuItem Spj;
    private javax.swing.JMenuItem abt;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JSeparator jSeparator5;
    private javax.swing.JMenuItem rmvComp;
    private javax.swing.JMenuItem showPtnBskt;
    // End of variables declaration


}